"""
Case Type   : gs_checkse工具功能
Case Name   : 验证gs_checkse的A2检测项确保pg_hba.conf文件权限最小化检测与修复成功
Create At   : 2024/10/24
Owner       : lihongji
Description :
    1.修改pg_hba.conf文件权限为0601后检测A2项
    2.修复A2不符合安全规范项目后再次检测
    3.修改pg_hba.conf文件权限为0602后检测A2项
    4.修复A2不符合安全规范项目后再次检测
    5.修改pg_hba.conf文件权限为0604后检测A2项
    6.修复A2不符合安全规范项目后再次检测
    7.修改pg_hba.conf文件权限为0610后检测A2项
    8.修复A2不符合安全规范项目后再次检测
    9.修改pg_hba.conf文件权限为0620后检测A2项
    10.修复A2不符合安全规范项目后再次检测
    11.修改pg_hba.conf文件权限为0640后检测A2项
    12.修复A2不符合安全规范项目后再次检测
    13.修改pg_hba.conf文件权限为0700后检测A2项
    14.修复A2不符合安全规范项目后再次检测
Expect      :
    1.修改目录权限成功并给出告警信息
    2.无告警信息
    3.修改目录权限成功并给出告警信息
    4.无告警信息
    5.修改目录权限成功并给出告警信息
    6.无告警信息
    7.修改目录权限成功并给出告警信息
    8.无告警信息
    9.修改目录权限成功并给出告警信息
    10.无告警信息
    11.修改目录权限成功并给出告警信息
    12.无告警信息
    13.修改目录权限成功并给出告警信息
    14.无告警信息
History     :
"""
import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH
        self.pg_hba_path = os.path.join(macro.DB_INSTANCE_PATH,
                                        'pg_hba.conf')

    def test_gscheckse(self):
        step = ('----step1:修改pg_hba.conf文件权限为0601后检测A2项  '
                'expect:修改目录权限成功并给出告警信息----')
        self.log.info(step)
        gs_checkse_cmd = (f"source {self.env_path};"
                          f"chmod 0601 {self.pg_hba_path};"
                          f"gs_checkse -i A2 --detail")
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("Ensure minimal permissions for the pg_hba.conf "
                      "file.", check_res, '执行失败' + step)

        step = '----step2:修复A2不符合安全规范项目后再次检测  expect:无告警信息----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B2 --detail;'
                          f'gs_checkse -i A2 --detail;'
                          f'find {self.pg_hba_path} \( ! -user '
                          f'{self.primaryNode.ssh_user} -o ! -group '
                          f'{self.primaryNode.ssh_user} -o -perm '
                          f'/u=x,g=rwx,o=rwx \)')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure minimal permissions for the pg_hba.conf "
                         "file.", check_res, '执行失败' + step)
        self.assertNotIn(self.pg_hba_path, check_res, '执行失败' + step)

        step = ('----step3:修改pg_hba.conf文件权限为0602后检测A2项  '
                'expect:修改目录权限成功并给出告警信息----')
        self.log.info(step)
        gs_checkse_cmd = (f"source {self.env_path};"
                          f"chmod 0602 {self.pg_hba_path};"
                          f"gs_checkse -i A2 --detail")
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("Ensure minimal permissions for the pg_hba.conf "
                      "file.", check_res, '执行失败' + step)

        step = '----step4:修复A2不符合安全规范项目后再次检测  expect:无告警信息----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B2 --detail;'
                          f'gs_checkse -i A2 --detail;'
                          f'find {self.pg_hba_path} \( ! -user '
                          f'{self.primaryNode.ssh_user} -o ! -group '
                          f'{self.primaryNode.ssh_user} -o -perm '
                          f'/u=x,g=rwx,o=rwx \)')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure minimal permissions for the pg_hba.conf "
                         "file.", check_res, '执行失败' + step)
        self.assertNotIn(self.pg_hba_path, check_res, '执行失败' + step)

        step = ('----step5:修改pg_hba.conf文件权限为0604后检测A2项  '
                'expect:修改目录权限成功并给出告警信息----')
        self.log.info(step)
        gs_checkse_cmd = (f"source {self.env_path};"
                          f"chmod 0604 {self.pg_hba_path};"
                          f"gs_checkse -i A2 --detail")
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("Ensure minimal permissions for the pg_hba.conf "
                      "file.", check_res, '执行失败' + step)

        step = '----step6:修复A2不符合安全规范项目后再次检测  expect:无告警信息----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B2 --detail;'
                          f'gs_checkse -i A2 --detail;'
                          f'find {self.pg_hba_path} \( ! -user '
                          f'{self.primaryNode.ssh_user} -o ! -group '
                          f'{self.primaryNode.ssh_user} -o -perm '
                          f'/u=x,g=rwx,o=rwx \)')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure minimal permissions for the pg_hba.conf "
                         "file.", check_res, '执行失败' + step)
        self.assertNotIn(self.pg_hba_path, check_res, '执行失败' + step)

        step = ('----step7:修改pg_hba.conf文件权限为0610后检测A2项  '
                'expect:修改目录权限成功并给出告警信息----')
        self.log.info(step)
        gs_checkse_cmd = (f"source {self.env_path};"
                          f"chmod 0610 {self.pg_hba_path};"
                          f"gs_checkse -i A2 --detail")
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("Ensure minimal permissions for the pg_hba.conf "
                      "file.", check_res, '执行失败' + step)

        step = '----step8:修复A2不符合安全规范项目后再次检测  expect:无告警信息----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B2 --detail;'
                          f'gs_checkse -i A2 --detail;'
                          f'find {self.pg_hba_path} \( ! -user '
                          f'{self.primaryNode.ssh_user} -o ! -group '
                          f'{self.primaryNode.ssh_user} -o -perm '
                          f'/u=x,g=rwx,o=rwx \)')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure minimal permissions for the pg_hba.conf "
                         "file.", check_res, '执行失败' + step)
        self.assertNotIn(self.pg_hba_path, check_res, '执行失败' + step)

        step = ('----step9:修改pg_hba.conf文件权限为0620后检测A2项  '
                'expect:修改目录权限成功并给出告警信息----')
        self.log.info(step)
        gs_checkse_cmd = (f"source {self.env_path};"
                          f"chmod 0620 {self.pg_hba_path};"
                          f"gs_checkse -i A2 --detail")
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("Ensure minimal permissions for the pg_hba.conf "
                      "file.", check_res, '执行失败' + step)

        step = '----step10:修复A2不符合安全规范项目后再次检测  expect:无告警信息----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B2 --detail;'
                          f'gs_checkse -i A2 --detail;'
                          f'find {self.pg_hba_path} \( ! -user '
                          f'{self.primaryNode.ssh_user} -o ! -group '
                          f'{self.primaryNode.ssh_user} -o -perm '
                          f'/u=x,g=rwx,o=rwx \)')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure minimal permissions for the pg_hba.conf "
                         "file.", check_res, '执行失败' + step)
        self.assertNotIn(self.pg_hba_path, check_res, '执行失败' + step)

        step = ('----step11:修改pg_hba.conf文件权限为0640后检测A2项  '
                'expect:修改目录权限成功并给出告警信息----')
        self.log.info(step)
        gs_checkse_cmd = (f"source {self.env_path};"
                          f"chmod 0640 {self.pg_hba_path};"
                          f"gs_checkse -i A2 --detail")
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("Ensure minimal permissions for the pg_hba.conf "
                      "file.", check_res, '执行失败' + step)

        step = '----step12:修复A2不符合安全规范项目后再次检测  expect:无告警信息----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B2 --detail;'
                          f'gs_checkse -i A2 --detail;'
                          f'find {self.pg_hba_path} \( ! -user '
                          f'{self.primaryNode.ssh_user} -o ! -group '
                          f'{self.primaryNode.ssh_user} -o -perm '
                          f'/u=x,g=rwx,o=rwx \)')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure minimal permissions for the pg_hba.conf "
                         "file.", check_res, '执行失败' + step)
        self.assertNotIn(self.pg_hba_path, check_res, '执行失败' + step)

        step = ('----step13:修改pg_hba.conf文件权限为0640后检测A2项  '
                'expect:修改目录权限成功并给出告警信息----')
        self.log.info(step)
        gs_checkse_cmd = (f"source {self.env_path};"
                          f"chmod 0700 {self.pg_hba_path};"
                          f"gs_checkse -i A2 --detail")
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("Ensure minimal permissions for the pg_hba.conf "
                      "file.", check_res, '执行失败' + step)

        step = '----step14:修复A2不符合安全规范项目后再次检测  expect:无告警信息----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B2 --detail;'
                          f'gs_checkse -i A2 --detail;'
                          f'find {self.pg_hba_path} \( ! -user '
                          f'{self.primaryNode.ssh_user} -o ! -group '
                          f'{self.primaryNode.ssh_user} -o -perm '
                          f'/u=x,g=rwx,o=rwx \)')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertNotIn("Ensure minimal permissions for the pg_hba.conf "
                         "file.", check_res, '执行失败' + step)
        self.assertNotIn(self.pg_hba_path, check_res, '执行失败' + step)

    def tearDown(self):
        step = '----step15:恢复环境 expect:成功----'
        self.log.info(step)
        recover = (f'source {self.env_path};'
                   f'chmod 0600 {self.pg_hba_path};'
                   f'find {self.pg_hba_path} \( ! -user '
                   f'{self.primaryNode.ssh_user} -o ! -group '
                   f'{self.primaryNode.ssh_user} -o -perm '
                   f'/u=x,g=rwx,o=rwx \)')
        self.log.info(recover)
        recover_res = self.common.get_sh_result(self.primaryNode, recover)
        self.assertNotIn(self.pg_hba_path, recover_res, '执行失败' + step)
        self.log.info(f'----{os.path.basename(__file__)} end----')